const token = localStorage.getItem('token')
token ? getUserInfo() : ''

axios.get('/index/index').then(({data:{data}, status}) => {
    // console.log(data, status)
    const {allCate,allLink} = data
    // 表头所有分类部分
    const id = localStorage.getItem('id')    
    const navLi = allCate.map(({id, icon, catename}, index) => {
        // console.log(id,catename)
        return `
        <li class="menu-item menu-item-type-taxonomy menu-item-object-category">
        <a href="./list.html?cateid=${id}&catename=${catename}">
        <span class="fa ${icon}"></span>${catename}</a></li>`  
    }).join('')
    // console.log(navLi)
    $('.nav').append(navLi)
    $('.nav li').eq(id).addClass('current_page_item').siblings().removeClass('current_page_item')
    // =============底部===================
    $('.lianjie').html(allLink.map(({url, des}) => {
        return `<a href="${url}" title="${des}">${des}</a> <span>|</span>`
    }))

    
    
})

$('.nav').on('click', 'li', function () {
    const id = $(this).index()
    const catename = $(this).text()
    localStorage.setItem('id', id)
    localStorage.setItem('catename', catename)
  })
// =============获取用户信息=========================
function getUserInfo(){
    axios.get('/user/getUserInfo',{params:{token:token}})
    .then(({data:{data,errmsg,errno}}) => {
      // console.log(username,errmsg,errno)
      if(errno){
        localStorage.removeItem('token')
        return $('.slinks').html(`<a href="login.html"><i class="fa fa-hand-o-right"></i>&nbsp;登录</a> |
        <a href="reg.html"><i class="fa fa-registered"></i>&nbsp;注册</a>`)
      }
      const {userInfo:{username}} = data
      $('.slinks').html(`<a href="./user.html"><i class="fa fa-user"></i>&nbsp;您好,${username}</a>
      <a href="javascript:;" class="fa fa-sign-out"></a>`)
    })
    // 设置点击退出
    $('body').on('click', '.fa-sign-out', () => {
      layer.confirm('确认退出吗?', { icon: 3, title: '提示' }, index => {
        localStorage.removeItem('token')
        location.href = './index.html'
        layer.close(index);
      });
    })
  }